<?php
namespace App\Admin\Actions\Grid\Mp;

use App\Lib\WeApp\WeApp;
use App\Models\Mp\WxMpFreePublish;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class FreePublishSubmitAction extends RowAction
{

    /**
     * 标题
     *
     * @return string
     */
    public function title()
    {
        if($this->row->parent_id === 0) {
            if (_empty_($this->row->article_id)) {
                return '发布文章';
            } else {
                if (in_array($this->row->publish_status, [2, 3, 4, 5, 6])) {
                    return '重新发布';
                } else if ($this->row->publish_status == 1) {
                    return '发布中，请等待';
                } else if ($this->row->publish_status === 0) {
                    return '删除文章';
                }
            }
        }else{
            $update_article_id = WxMpFreePublish::where('id', $this->row->parent_id)->value('article_id');
            if ($update_article_id) {
                return '删除子级文章';
            }
        }
        return '';
    }

    /**
     * 设置确认弹窗信息，如果返回空值，则不会弹出弹窗
     *
     * 允许返回字符串或数组类型
     *
     * @return array|string|void
     */
    public function confirm()
    {
        if($this->row->parent_id === 0) {
            if (_empty_($this->row->article_id)) {
                return [
                    // 确认弹窗 title
                    "确定新增一个公众号文章吗？",
                    // 确认弹窗 content
                    $this->row->title,
                ];
            } else {
                if (in_array($this->row->publish_status, [2, 3, 4, 5, 6])) {
                    return [
                        // 确认弹窗 title
                        "确定重新发布这个公众号文章吗？",
                        // 确认弹窗 content
                        $this->row->title,
                    ];
                } else if ($this->row->publish_status == 1) {
                    return [
                        // 确认弹窗 title
                        "已在发布中，请等待微信发布完成",
                        // 确认弹窗 content
                        $this->row->title,
                    ];
                } else if ($this->row->publish_status === 0) {
                    return [
                        // 确认弹窗 title
                        "确定删除微信公众号的这篇文章？",
                        // 确认弹窗 content
                        $this->row->title,
                    ];
                }
            }
        }else{
            $update_article_id = WxMpFreePublish::where('id', $this->row->parent_id)->value('article_id');
            if ($update_article_id) {
                return [
                    // 确认弹窗 title
                    "确定删除这个微信公众号的子级文章？",
                    // 确认弹窗 content
                    $this->row->title,
                ];
            }
        }
        return '';
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return \Dcat\Admin\Actions\Response
     */
    public function handle(Request $request)
    {
        // 获取当前行ID
        $id = $this->getKey();
        $free_publish = WxMpFreePublish::find($id);

        $weapp = new WeApp('mp');
        $mp = $weapp->getMpServicer();

        if($free_publish->parent_id === 0) {
            if (_empty_($free_publish->article_id)) {
                $res = $mp->freePublishSubmit($free_publish->id);
            } else{
                if (in_array($free_publish->publish_status, [2, 3, 4, 5, 6])) {
                    $res = $mp->freePublishSubmit($free_publish->id);
                } else if ($free_publish->publish_status == 1) {
                    return $this->response()->warning("已在发布中，请等待微信发布完成");
                } else if ($free_publish->publish_status === 0) {
                    $res = $mp->deleteFreePublish($free_publish->article_id);
                }
            }
        }else{
            // 删除子级文章
            $update_index = 0;
            $update_article_id = WxMpFreePublish::where('id', $free_publish->parent_id)->value('article_id');
            if(_empty_($update_article_id)){
                return $this->response()->warning("还没有发布");
            }
            if($free_publish->parent_id > 0) {
                $pluck_ids = WxMpFreePublish::where('parent_id', $free_publish->parent_id)->orderBy('order', 'asc')->orderBy('id', 'asc')->pluck('id')->toArray();
                if ($pluck_ids) {
                    $update_index = array_search($free_publish->id, $pluck_ids) + 1;
                }
            }
            $res = $mp->deleteFreePublish($update_article_id, $update_index);
        }

        if($res['code'] == 200){
            return $this->response()->success("操作成功")->refresh();
        }else{
            return $this->response()->error($res['msg']);
        }
    }

    /**
     * 设置要POST到接口的数据
     *
     * @return array
     */
    public function parameters()
    {
        return [
            'title' => $this->row->title,
        ];
    }
}
